package com.woniu.gf.pawn.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.gf.pawn.comm.core.domain.Image;
import com.woniu.gf.pawn.comm.core.domain.ProductSmallType;
import com.woniu.gf.pawn.comm.core.util.OssUntil;
import com.woniu.gf.pawn.dao.ImageMapper;
import com.woniu.gf.pawn.dao.ProductSmallTypeMapper;
import com.woniu.gf.pawn.product.parma.FindProductSmallTypePageParam;
import com.woniu.gf.pawn.product.parma.ShowProductSmallTypeParam;
import com.woniu.gf.pawn.service.IProductSmallTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2020-10-15
 */
@Service
public class ProductSmallTypeServiceImpl extends ServiceImpl<ProductSmallTypeMapper, ProductSmallType> implements IProductSmallTypeService {

    @Autowired
    private ProductSmallTypeMapper productSmallTypeMapper;

    @Autowired
    private ImageMapper imageMapper;

    /**
     * 分页查询
     * @param pages
     * @return
     */
    @Override
    public IPage<ShowProductSmallTypeParam> selectPageVo(FindProductSmallTypePageParam pages) {
        Page<ShowProductSmallTypeParam> bigTypePage = new Page<ShowProductSmallTypeParam>(pages.getIndex(), pages.getSize());
        IPage<ShowProductSmallTypeParam> productSmallTypeIPage = productSmallTypeMapper.selectPageVo(bigTypePage,pages.getBigId(),pages.getName());
        return productSmallTypeIPage;
    }

    /**
     * 添加
     * @param productSmallType
     * @param image
     */
    @Override
    @Transactional
    public void addSmallType(ProductSmallType productSmallType, Image image) {
        imageMapper.insert(image);
        productSmallType.setImageId(image.getImageId());
        productSmallTypeMapper.insert(productSmallType);
    }

    /**
     * 修改
     * @param productSmallType
     * @param image
     */

    @Override
    public void updateSmallType(ProductSmallType productSmallType, Image image) {
       if(image!=null&&image.getImageAddr()!=null){
           imageMapper.updateById(image);
       }
        productSmallTypeMapper.updateById(productSmallType);
    }

    /**
     * 删除
     * @param ids
     */
    @Override
    @Transactional
    public  void deleteSmallType(List<Integer> ids) {

        productSmallTypeMapper.deleteBatchIds(ids);
        List<Map<Integer, String>> maps = imageMapper.deleteSmallImage();
        if(maps.size()>0){
            List<Integer> list=new ArrayList<>();
            for (int i=0;i<maps.size();i++){
                for (Integer key : maps.get(i).keySet()) {
                    list.add(key);
                    OssUntil.deleteObject(maps.get(i).get(key));
                }
            }
            imageMapper.deleteBatchIds(list);
        }
    }
}
